package mysql

import (
	"video/videorpc/videorpc"

	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	VideoId       int    `gorm:"type:int(11);not null"`
	UserId        int    `gorm:"type:int(11);not null"`
	Content       string `gorm:"type:varchar(500);not null"`
	IsAnonymous   int    `gorm:"tinyint:int(11);default:0"`
	CommentStatus int    `gorm:"tinyint:int(11);default:0"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) CommentDel(db *gorm.DB, UserId int64, VideoId int64) error {
	return db.Debug().Where("user_id=?", UserId).Where("video_id=?", VideoId).Delete(c).Error
}

func (c *Comment) FindComment(db *gorm.DB, UserId int64, VideoId int64) error {
	return db.Debug().Where("user_id=?", UserId).Where("video_id=?", VideoId).Find(c).Error
}

func CommentList(db *gorm.DB, in *videorpc.CommentListRequest) []*videorpc.CommentList {
	/*
		comments . *,
		users.avatar_link,
		users.nick_name
		 FROM
		  comments
		LEFT JOIN users ON comments.user_id=users.id
	*/
	var list []*videorpc.CommentList
	db.Debug().Model(&Comment{}).
		Select("comments . *,users.avatar_link,users.nick_name").
		Joins("LEFT JOIN users ON comments.user_id=users.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list)
	return list
}
